package com.hjq.demo.yuliao

import android.app.Activity
import android.content.*
import android.os.Bundle
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.dylanc.mmkv.MMKVOwner
import com.gyf.immersionbar.ImmersionBar
import com.hjq.base.FragmentPagerAdapter
import com.hjq.demo.R
import com.hjq.demo.app.vb.AppVbActivity
import com.hjq.demo.databinding.ActivityMainBinding
import com.hjq.demo.http.model.HttpData
import com.hjq.demo.manager.*
import com.hjq.demo.other.DoubleClickHelper
import com.hjq.demo.ui.adapter.NavigationAdapter
import com.hjq.demo.ui.fragment.FindFragment
import com.hjq.demo.ui.fragment.HomeFragment
import com.hjq.demo.ui.fragment.MessageFragment
import com.hjq.demo.ui.fragment.MineFragment
import com.hjq.demo.yuliao.api.GetUserinfoApi
import com.hjq.demo.yuliao.fragment.HomeBangDanFragment
import com.hjq.demo.yuliao.fragment.HomeMainFragment
import com.hjq.demo.yuliao.fragment.HomeMainMessageFragment
import com.hjq.demo.yuliao.fragment.HomeMainMineFragment
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import io.rong.imlib.IRongCoreCallback
import io.rong.imlib.IRongCoreEnum
import io.rong.imlib.RongCoreClient
import okhttp3.internal.http.HttpDate


/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject-Kotlin
 *    time   : 2018/10/18
 *    desc   : 首页界面
 */
class MainActivity : AppVbActivity<ActivityMainBinding>(), NavigationAdapter.OnNavigationListener, MMKVOwner {

    companion object {

        private const val INTENT_KEY_IN_FRAGMENT_INDEX: String = "fragmentIndex"
        private const val INTENT_KEY_IN_FRAGMENT_CLASS: String = "fragmentClass"

        @JvmOverloads
        fun start(context: Context, fragmentClass: Class<out Fragment>? = HomeMainFragment::class.java) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra(INTENT_KEY_IN_FRAGMENT_CLASS, fragmentClass)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }

    private val viewPager: ViewPager? by lazy { findViewById(R.id.vp_home_pager) }
    private val navigationView: RecyclerView? by lazy { findViewById(R.id.rv_home_navigation) }
    private var navigationAdapter: NavigationAdapter? = null
    private var pagerAdapter: FragmentPagerAdapter<Fragment>? = null

    override fun initView() {

        navigationAdapter = NavigationAdapter(this).apply {
            addItem(
                NavigationAdapter.MenuItem(
                    getString(R.string.main_home),
                    ContextCompat.getDrawable(this@MainActivity, R.drawable.home_home_selector)
                )
            )
            addItem(
                NavigationAdapter.MenuItem(
                    getString(R.string.main_list),
                    ContextCompat.getDrawable(this@MainActivity, R.drawable.home_found_selector)
                )
            )
            addItem(
                NavigationAdapter.MenuItem(
                    getString(R.string.main_message),
                    ContextCompat.getDrawable(this@MainActivity, R.drawable.home_message_selector)
                )
            )
            addItem(
                NavigationAdapter.MenuItem(
                    getString(R.string.main_mine),
                    ContextCompat.getDrawable(this@MainActivity, R.drawable.home_me_selector)
                )
            )
            setOnNavigationListener(this@MainActivity)
            navigationView?.adapter = this
        }
    }

    override fun initData() {
        pagerAdapter = FragmentPagerAdapter<Fragment>(this).apply {
            addFragment(HomeMainFragment.newInstance())
            addFragment(HomeBangDanFragment.newInstance())
            addFragment(HomeMainMessageFragment.newInstance())
            addFragment(HomeMainMineFragment.newInstance())
            viewPager?.adapter = this
        }
        onNewIntent(intent)

        getUserData()
    }

    fun getUserData() {
        EasyHttp.post(this)
            .api(GetUserinfoApi())
            .request(object : HttpCallback<HttpData<GetUserinfoApi.Response?>>(this) {
                override fun onSucceed(result: HttpData<GetUserinfoApi.Response?>?) {

                    kv.encode("userinfo", result?.getData()?.user_info)
                }
            })
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        pagerAdapter?.let {
            switchFragment(it.getFragmentIndex(getSerializable(INTENT_KEY_IN_FRAGMENT_CLASS)))
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        viewPager?.let {
            // 保存当前 Fragment 索引位置
            outState.putInt(INTENT_KEY_IN_FRAGMENT_INDEX, it.currentItem)
        }
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        // 恢复当前 Fragment 索引位置
        switchFragment(savedInstanceState.getInt(INTENT_KEY_IN_FRAGMENT_INDEX))
    }

    private fun switchFragment(fragmentIndex: Int) {
        if (fragmentIndex == -1) {
            return
        }
        when (fragmentIndex) {
            0, 1, 2, 3 -> {
                viewPager?.currentItem = fragmentIndex
                navigationAdapter?.setSelectedPosition(fragmentIndex)
            }
        }
    }

    /**
     * [NavigationAdapter.OnNavigationListener]
     */
    override fun onNavigationItemSelected(position: Int): Boolean {
        return when (position) {
            0, 1, 2, 3 -> {
                viewPager?.currentItem = position
                true
            }
            else -> false
        }
    }

    override fun createStatusBarConfig(): ImmersionBar {
        return super.createStatusBarConfig() // 指定导航栏背景颜色
            .navigationBarColor(R.color.white)
    }

    override fun onBackPressed() {
        if (!DoubleClickHelper.isOnDoubleClick()) {
            toast(R.string.home_exit_hint)
            return
        }

        // 移动到上一个任务栈，避免侧滑引起的不良反应
        moveTaskToBack(false)
        postDelayed({
            // 进行内存优化，销毁掉所有的界面
            ActivityManager.getInstance().finishAllActivities()
        }, 300)
    }

    override fun onDestroy() {
        super.onDestroy()
        viewPager?.adapter = null
        navigationView?.adapter = null
        navigationAdapter?.setOnNavigationListener(null)
    }
}